/*
 * This file is part of LangMan.
 *
 * LangMan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * LangMan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LangMan.  If not, see <http://www.gnu.org/licenses/>.
 */

package langman.gui.translation;

import javafx.fxml.FXML;
import javafx.scene.control.*;
import langman.business.data.TranslationService;
import langman.business.data.model.Key;
import langman.business.data.model.Translation;
import langman.gui.tree.KeyItem;
import langman.gui.tree.TreeItemSelectionService;
import langman.util.doping.Bean;
import langman.util.doping.Inject;
import langman.util.guifactory.AbstractFXMLBeanControl;

@Bean
public class TranslationsTable extends AbstractFXMLBeanControl {

    private final TranslationService translationService;
    private final TreeItemSelectionService treeItemSelectionService;

    @FXML
    private TableView<Translation> translationTable;

    @FXML
    private TableColumn<Translation, String> languageColumn;

    @FXML
    private TableColumn<Translation, String> translationColumn;

    @FXML
    private TextField keyTextField;

    @FXML
    private TextField localStringTextField;

    @FXML
    private CheckBox dontTranslateCheckBox;

    @Inject
    public TranslationsTable(TranslationService translationService, TreeItemSelectionService treeItemSelectionService) {
        this.translationService = translationService;
        this.treeItemSelectionService = treeItemSelectionService;
    }

    @Override
    public void initializeAfterInjection() {
        treeItemSelectionService.selectedItemProperty().addListener((observableValue, oldValue, treeItem2) -> {
            unbindOld(oldValue);
            updateGUI();
        });
        updateGUI();


        translationTable.editableProperty().bind(dontTranslateCheckBox.selectedProperty());
        translationTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
        languageColumn.setCellValueFactory(cellDataFeatures -> cellDataFeatures.getValue().languageNameProperty());
        languageColumn.setSortable(false);

        translationColumn.setCellFactory(translationStringTableColumn -> new TranslationEditingCell());
        translationColumn.editableProperty().bind(dontTranslateCheckBox.selectedProperty());
        translationColumn.setSortable(false);
    }

    private void unbindOld(TreeItem oldValue) {
        if (oldValue instanceof KeyItem) {
            Key key = ((KeyItem) oldValue).getKey();
            key.dontTranslateProperty().unbindBidirectional(dontTranslateCheckBox.selectedProperty());
        }
    }

    @SuppressWarnings("rawtypes")
    private void updateGUI() {
        TreeItem selected = treeItemSelectionService.selectedItemProperty().get();
        if (selected instanceof KeyItem) {
            Key key = ((KeyItem) selected).getKey();

            translationTable.setItems(translationService.createTranslations(key));
            translationTable.requestLayout();

            String keyText = key.getKey();
            keyTextField.setText(keyText);
            localStringTextField.setText(String.format(".getLocalString(\"%s\");", keyText));

            dontTranslateCheckBox.selectedProperty().bindBidirectional(key.dontTranslateProperty());
        } else {
            translationTable.setItems(null);
        }

    }
}
